function data_customized_entry(sys_table_name,rel_key){
	this.sys_table_name = sys_table_name;
	this.rel_key = rel_key ;
	this.rel_entries = [];
	this.add_relation_entry =function(rel_entry){
		var len = this.rel_entries.length ;
		var temp ;
		var exist =false; 
		for(var i=0;i<len;i++) {
			temp = this.rel_entries[i];
			if(temp.from_ds == rel_entry.from_ds &&
				temp.tb_name == rel_entry.tb_name) {
				this.rel_entries[i].addColumns(rel_entry.coloums) ;
				exist = true ;
				break;	
			}
		}
		if(!exist) {
			this.rel_entries[len]=rel_entry;
		}
	}
	this.toString=function() {
		var str = "{\"relDts\":[" ;
		var len = this.rel_entries.length ;
		for(var i=0;i<len-1;i++) {
			str += this.rel_entries[i].toString() + ",";
		}
		str+=this.rel_entries[len-1].toString() +"],\"relationId\":\""+null+"\",\"tableName\":\""+this.sys_table_name+"\",\"relationKey\":\""
			+this.rel_key+"\"}";
		return str;
		
	}
}

function relation_entry(from_ds,tb_name,rl_key){
	this.from_ds = from_ds;
	this.tb_name = tb_name ;
	this.rl_key = rl_key;
	this.coloums = [];
	this.getLength=function(){
		return this.coloums.length;
	}
	
	this.addColoum=function(column) {
		this.coloums[this.getLength()] = column;
	};
	
	this.addColumns=function(columns) {
		var len = this.getLength() ;
		for(var i=0;i<columns.length;i++) {
			this.coloums[len++] = columns[i];
		}
	}
	
	this.toString=function() {
		var len = this.coloums.length;
		var str = "{\"columns\":{" ;
		for(var i=0;i<len-1;i++) {
			str+= this.coloums[i].toString()+",";
		}
		str+=this.coloums[len-1]+"},\"fromDateSourceId\":\""+this.from_ds + "\","+
			"\"fromTableName\":\"" + this.tb_name+"\","+
			"\"relationKey\":\"" + this.rl_key+"\"}";
		return str;
		
	}
}

function columns_field(name,type) {
	this.name = name ;
	this.type = type ;
	this.toString=function() {
		return "\""+this.name+"\""+":"+"\""+this.type+"\"" ;
	}
}


function submit_customized() {
	
	showBlock("table.table1");
	var sel_fields = $("#select_field a") ;
	var len = sel_fields.length ;
	var da = new data_customized_entry($("#sys-tab").attr("value"),$("#sys-column").attr("value"));
	
	for(var i=0;i<len;i++) {
		var temp = $(sel_fields[i]);
		var rel = new relation_entry(temp.attr("from_ds"),temp.attr("tb_name"),$("#sys-column").attr("value"));
		var r = new columns_field(temp.html(), temp.attr("value"));
		rel.addColoum(r);
		da.add_relation_entry(rel);
	}
	SystemService.submitCustomized(da.toString(),function(data){ 
			$("table.table1").unblock();
　　　　　　　　　　	setTimeout(function(){
				var result = data.split(":");
				if(result[0] == "success") {
					alert(result[1]);
					go_on_customized();
				}else {
					alert(result[1]);
				}
			},500);
　　　　　　　 });
}
function go_on_customized() {
	delete_table_rows("fieldTable");
	clear_select_field();
	$("#ds_table").attr("value",'-1');
}
function delete_table_rows(id) {
	var tb = "#" + id + " tbody";
	$(tb).html("");
}
function clear_select_field() {
	$("#select_field").html("");
}
function add_select_field(name, type) {
	var field = "<a href='javascript:void()' class='mybutton' value='" + type + "' id='field'>" + name + "</a>";
	$("#select_field").append(field);
}
function getTables() {
	var value = $("#datasources").attr("value");
	var ds_table = $("#ds_table");
	ds_table.empty();
	ds_table.append("<option value=\"-1\">----------------\u8bf7\u9009\u62e9---------------</option>");
	$("#aa").empty();
	if (value != "-1") {
		showBlock("table.table1");
		SystemService.getTablesByDsId(value, function (data) {
			if (data) {
				for (var i = 0; i < data.length; i++) {
					ds_table.append("<option value='" + data[i] + "'>" + data[i] + "</option>");
				}
			}
			$("table.table1").unblock();
		});
	}
}
function getFields() {
	var ds_id = $("#datasources").attr("value");
	var tb_name = $("#ds_table").attr("value");
	var tab = $("#aa");
	tab.empty();
	if (ds_id != "-1" && tb_name != "-1") {
		showBlock("table.table1");
		SystemService.getAllColumsByTableName(tb_name, ds_id, function (data) {
			if (data) {
				for (attribute in data) {
					var checked = "";
					var id = $.trim(((ds_id + "" + tb_name + "" + attribute).replace(/\\$/g, "")).replace(/_/g, ""));
					var checked = "";
					if ($("#" + id, $("#select_field")).length > 0) {
						checked = "checked=checked";
					}
					var a_tr = "<tr>" + "<td><input type=\"checkbox\" id='" + id + "' column_type='" + data[attribute] + "' value='" + attribute + "' ds_id='" + ds_id + "' tb_name='" + tb_name + "' onclick='select_field(this)' " + checked + " /></td>" + "<td>" + attribute + "</td> " + "<td>" + data[attribute] + "</td>" + "</tr>";
					tab.append(a_tr);
				}
			}
			$("table.table1").unblock();
		});
	}
}
function select_field(ck) {
	var checkBoX = $(ck);
	if (checkBoX.attr("checked")) {
		var field = "<a href='javascript:void()' class='mybutton' from_ds='"+checkBoX.attr("ds_id")+"' tb_name='"+checkBoX.attr("tb_name")+"' value='" + checkBoX.attr("column_type") + "' id='" + checkBoX.attr("id") + "'>" + checkBoX.attr("value") + "</a>";
		$("#select_field").append(field);
	} else {
		$("#" + $.trim(ck.id), $("#select_field")).remove();
	}
}
function showBlock(id) {
	$(id).block({message:"<h3>Loading</h3>", css:{border:"2px solid black"}});
}
function getSystemTables() {
	SystemService.getSystemTables(function (data) {
		if (data) {
			var sys = $("#sys-tab");
			for (var i = 0; i < data.length; i++) {
				sys.append("<option value='" + data[i] + "'>" + data[i] + "</option>");
			}
		}
	});
}
function getSystemColumns() {
	var tbName = $("#sys-tab").attr("value");
	var sys_columns = $("#sys-column");
	sys_columns.empty();
	if (tbName != "-1") {
		showBlock("table.table1");
		SystemService.getColumnsByTableName(tbName, function (data) {
			if (data) {
				for (attribute in data) {
					sys_columns.append("<option value='" + attribute + "'>" + attribute + "</option>");
				}
			}
			$("table.table1").unblock();
		});
	}
}



